<?php

namespace app\admin\controller;

use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\zbtask\Money;
use app\common\model\zbtask\Order;
use app\common\model\zbtask\Zbtask;
use app\common\utils\Utils;
use think\Config;

/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        $uploadmode = isset($hooks['upload_config_init']) && $hooks['upload_config_init'] ? implode(',', $hooks['upload_config_init']) : 'local';
        $addonComposerCfg = ROOT_PATH . '/vendor/karsonzhang/fastadmin-addons/composer.json';
        Config::parse($addonComposerCfg, "json", "composer");
        $config = Config::get("composer");
        $addonVersion = isset($config['version']) ? $config['version'] : __('Unknown');
        $this->view->assign([
            'addonversion'       => $addonVersion,
            'uploadmode'       => $uploadmode
        ]);


        $allUsers = User::where(['status'=>'normal'])->count();  //总会员数量
        $allCompany = \app\common\model\Company::count();
        //统计
        $allService = Money::where([
            'status' => 1
        ])->sum('service_money');//总有效服务费

        $allOrders = Zbtask::where([
            'status' => 1,
        ])->count();  //总结算订单数

        $allMoney = Money::where([
            'status' => 1
        ])->sum('Money');//总应发
        $allReal = Money::where([
            'status' => 1
        ])->sum('get_money');//总实发
        $allRate = Money::where([
            'status' => 1
        ])->sum('already_money');//总纳税金额
        $allTaskaa = Zbtask::where(['type'=> 1])->count(); //总任务数量
        $usersStatics = [];
        $taskStatics = [];
        //echarts 总用户统计
        for($i = 1; $i <= 12; $i++) {
            if ($i == 1) {
                $start = (1-$i). ' months 00:00:00';
                $end = 'now';
            } else {
                $start = (1-$i). ' months 00:00:00';
                $end = (2-$i). ' months 00:00:00';
            }
            $startTime = strtotime($start);
            $endTime = strtotime($end);
            $allUser = User::where([
                'status'=> 'normal',
                'createtime' => ['between', [$startTime, $endTime]]
            ])->count();
            $allTasks = Zbtask::where([
                'createtime'=> ['between', [$startTime, $endTime]],
                'type'=> 1
            ])->count();
            $startFormat = Utils::time_format($startTime, 'Y-m');
            $usersStatics[$startFormat] = $allUser;

            $taskStatics[$startFormat] = $allTasks;
        }

        $this->assign('top_statics', [
            'allUsers' => $allUsers,
            'allCompany' => $allCompany,
            'allOrders' => 0,
            'allService' => $allService,
            'allMoney' => $allMoney,
            'allReal' => $allReal,
            'allRate' => $allRate,
            'allTaskaa' => $allTaskaa,
        ]);
        $usersStatics = array_reverse($usersStatics);
        $taskStatics = array_reverse($taskStatics);
        //折线图
        $this->assign([
            'usersStatics' => $usersStatics,
            'taskStatics' => $taskStatics
        ]);

        //左边的图
        $todayRegister = User::where(['status'=>'normal'])->whereTime('createtime', 'd')->count();//今天注册数量
        $todayLogin = User::where(['status'=>'normal'])->whereTime('logintime', 'd')->count(); //今天登陆
        $todayAllOrders = Order::where([
            'status' => 1,
        ])->whereTime('createtime', 'd')->count();  //今日
        $todayNoProcess = Order::where([
            'status' => 1,
            'type' => 0,
        ])->whereTime('createtime', 'd')->count();  //今日

        //七日新增用户
        $weekAdd = User::where(['status'=>'normal'])->whereTime('createtime', '-7 days')->count(); //今天登陆
        $weeklog = User::where(['status'=>'normal'])->whereTime('logintime', '-7 days')->count(); //今天登陆

        $this->assign('rdata', [
            'todayRegister' =>$todayRegister,
            'todayLogin' =>$todayLogin,
            'todayAllOrders' =>$todayAllOrders,
            'todayNoProcess' =>$todayNoProcess,
            'weekAdd' =>$weekAdd,
            'weeklog' =>$weeklog,
        ]);

        //任务详情
        $statics = (new Zbtask())->getStatics();
        $this->assign('statics', $statics);
        return $this->view->fetch();
    }


    public function newTask() {
        $list = (new Zbtask())->order('createtime desc')->limit(15)->select();
        foreach ($list as &$v) {
            $v['url'] = url('zb.task/index', ['id'=> $v['id']]);
        }
        $this->success('最新任务', url('zb/task'), $list);
    }

    public function money() {
        $map = [];

        $list = (new Money())->order('createtime desc')->limit(15)->select();
        foreach ($list as &$v) {
            $v['url'] = url('zbtask/money/index', ['id'=> $v['id']]);
            $v['username'] = User::where(['id'=>$v['user_id']])->value('real_name');
            $v['createtime_text'] = Utils::time_format($v['createtime']);
        }

        $this->success('最新任务', '', $list);
    }
}
